//
//  PlayerUtils.swift
//  videoPlayerSwift
//
//  Created by xwl on 2019/3/7.
//  Copyright © 2019年 xuwanliang. All rights reserved.
//

import Foundation

func getAllFilePath(_ dirPath: String) -> [String]? {
    var filePaths = [String]()
    
    do {
        let array = try FileManager.default.contentsOfDirectory(atPath: dirPath)
        
        for fileName in array {
            var isDir: ObjCBool = true
            
            let fullPath = "\(dirPath)/\(fileName)"
            
            if FileManager.default.fileExists(atPath: fullPath, isDirectory: &isDir) {
                if !isDir.boolValue {
                    filePaths.append(fullPath)
                }
            }
        }
    } catch let error as NSError {
        print("get file path error: \(error)")
    }
    return filePaths;
}

/// (recommended) 获取指定文件夹下符合扩展名要求的所有文件名
/// - parameter path: 文件夹路径
/// - parameter filterTypes: 扩展名过滤类型(注：大小写敏感)
/// - returns: 所有文件名数组
func findFiles(path: String, filterTypes: [String]) -> [String] {
    do {
        let files = try FileManager.default.contentsOfDirectory(atPath: path)
        if filterTypes.count == 0 {
            return files
        }
        else {
            let filteredfiles = NSArray(array: files).pathsMatchingExtensions(filterTypes)
            return filteredfiles
        }
    }
    catch {
        return []
    }
}
